/*******************************************************************************
Project: The Impact of Dodd-Frank and the Huawei Shock on DRC Tin Exports
File: Analysis.do
Description: This do-file replicates the main structural estimations for the 
             export demand elasticity (Table 1) and the inverse supply elasticity 
             (Table A1) using the integrated dataset (DRC + Congo-Brazzaville).
Input: Data_Integrated.csv (generated by Data_Integration.do)
*******************************************************************************/


clear all
set more off

* Set working directory to the location of this do-file
* cd "`c(filedir)'"

* Load Data
import delimited "Data_Integrated.csv", clear varnames(1)

* ==============================================================================
* 1. Data Preparation
* ==============================================================================

* Generate Variables
* Log of Export Value (Dependent Variable)
gen ln_Value = ln(tradevalue)

* Bilateral Exchange Rate (Endogenous Variable)
* Definition: Currency j / COD = [Currency_j/USD] / [CDF/USD]
gen FX_j0 = rfx / pfx
gen ln_FX_j0 = ln(FX_j0)

* Third-country Exchange Rate (Instrumental Variable)
* Definition: Currency k / COD = [Currency_k/USD] / [CDF/USD]
gen FX_k0 = ivfx / pfx
gen ln_FX_k0 = ln(FX_k0)

* Control Variables and IV
gen ln_ivfx = ln(ivfx)
gen ln_lme = ln(lme)

* Set Panel Structure
* Create numeric year and month from 'month_period' (e.g., 201005)
gen year_val = floor(month_period / 100)
gen month_val = mod(month_period, 100)
gen year_month = ym(year_val, month_val)
format year_month %tm
drop reporter_id
encode reportercode, gen(reporter_id)

* Calculate Unit Value (UV)
* tradevalue (USD) / quantity (tons) * pfx (COD/USD) = COD/ton
gen UV_0 = (tradevalue/quantity)*pfx 
gen ln_UV_0 = ln(UV_0)
gen lnfrommmr = ln(frommmr)

* Declare Panel Data
xtset reporter_id year_month


* ==============================================================================
* 2. MAIN ANALYSIS: TABLE 1
* Estimation of Export Demand Elasticity over Distinct Periods
* ==============================================================================

* ------------------------------------------------------------------------------
* Period 1: Before Regulation (<= July 2012)
* Corresponds to Table 1, Column (1)
* ------------------------------------------------------------------------------
xtivreg2 ln_Value (ln_FX_j0 = FX_k0 ivfx) ln_lme if month_period <= 201207, fe robust bw(2)
nlcom (eta: _b[ln_FX_j0] - 1) // Test H0: eta = 0 (beta = 1)


* ------------------------------------------------------------------------------
* Period 2: Dodd-Frank Era (August 2012 - April 2017)
* Corresponds to Table 1, Column (2)
* ------------------------------------------------------------------------------
xtivreg2 ln_Value (ln_FX_j0 = ln_FX_k0 ivfx) ln_lme if month_period >= 201208 & month_period <= 201704, fe robust bw(2)
nlcom (eta: _b[ln_FX_j0] - 1) // Test H0: eta = 0 (beta = 1)


* ------------------------------------------------------------------------------
* Period 3: Transition (May 2017 - May 2019)
* Corresponds to Table 1, Column (3)
* ------------------------------------------------------------------------------
xtivreg2 ln_Value (ln_FX_j0 = ln_FX_k0 ivfx) ln_lme if month_period >= 201705 & month_period <= 201905, fe robust bw(2)
nlcom (eta: _b[ln_FX_j0] - 1) // Test H0: eta = 0 (beta = 1)


* ------------------------------------------------------------------------------
* Period 4: Huawei Shock and Market Recovery (June 2019 - October 2022)
* Corresponds to Table 1, Column (4)
* Note: Ends in Oct 2022 to isolate the recovery mechanism from 
* subsequent global macroeconomic shocks.
* ------------------------------------------------------------------------------
xtivreg2 ln_Value (ln_FX_j0 = ln_FX_k0 ln_ivfx) ln_lme if month_period >= 201906 & month_period <= 202210, fe robust bw(2)
nlcom (eta: _b[ln_FX_j0] - 1) // Test H0: eta = 0 (beta = 1)


* ==============================================================================
* 3. APPENDIX ANALYSIS: TABLE A1
* Estimation of Inverse Supply Elasticity (Omega)
* ==============================================================================

* ------------------------------------------------------------------------------
* Overall Sample (Periods 1 to 4: <= October 2022)
* Tests the identifying assumption that supply is highly elastic (omega approx 0)
* Corresponds to Appendix Table A1
* ------------------------------------------------------------------------------
ivreg2 ln_UV_0 (ln_Value = FX_j0 L.FX_j0) ln_lme if month_period <= 202210, bw(2) robust 


* ==============================================================================
* 4. ROBUSTNESS CHECKS FOR REFEREE #1
* Controlling for Myanmar Tin Imports (Table B1)
* ==============================================================================

* ------------------------------------------------------------------------------
* Period 3 (May 2017 - May 2019) - Myanmar Control
* ------------------------------------------------------------------------------
xtivreg2 ln_Value (ln_FX_j0 = ln_FX_k0 ivfx) ln_lme if lnfrommmr !=. & month_period >= 201705 & month_period <= 201905, fe robust bw(2)
nlcom (eta: _b[ln_FX_j0] - 1)
xtivreg2 ln_Value (ln_FX_j0 = ln_FX_k0 ivfx) ln_lme lnfrommmr if month_period >= 201705 & month_period <= 201905, fe robust bw(2)
nlcom (eta: _b[ln_FX_j0] - 1)


* ------------------------------------------------------------------------------
* Period 4 (June 2019 - October 2022) - Myanmar Control
* ------------------------------------------------------------------------------
xtivreg2 ln_Value (ln_FX_j0 = ln_FX_k0 ln_ivfx) ln_lme if lnfrommmr !=. & month_period >= 201906 & month_period <= 202210, fe robust bw(2)
nlcom (eta: _b[ln_FX_j0] - 1)
xtivreg2 ln_Value (ln_FX_j0 = ln_FX_k0 ln_ivfx) ln_lme lnfrommmr if month_period >= 201906 & month_period <= 202210, fe robust bw(2)
nlcom (eta: _b[ln_FX_j0] - 1)


* ------------------------------------------------------------------------------
* End of Do-File
* ------------------------------------------------------------------------------
